package org.goj.user.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.goj.common.constant.ResultCode;
import org.goj.common.entity.user.Team;
import org.goj.common.model.Result;
import org.goj.feign.feign.BaseFeignClient;
import org.goj.user.service.TeamService;
import org.goj.user.vo.TeamAdminVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 团队表 前端控制器
 * </p>
 *
 * @author CK
 * @since 2021-05-05
 */
@RestController
@RequestMapping("/team")
public class TeamController {

    @Resource
    private TeamService teamService;

    @Autowired
    private BaseFeignClient baseFeignClient;

    /**
     * 管理员添加或更新团队
     *
     * @param userId      添加者ID
     * @param title       标题
     * @param description 描述
     * @param status      状态
     * @param privilege   权限
     * @param emails      用户邮箱集合
     * @param teamId      团队ID
     * @return Result<Boolean>
     * @author CK
     * @date 2021/5/5 17:22
     */
    @PostMapping("/insertOrUpdateTeam")
    public Result<Boolean> insertTeam(
            @RequestParam(value = "userId") Integer userId,
            @RequestParam(value = "title") String title,
            @RequestParam(value = "description") String description,
            @RequestParam(value = "status") Boolean status,
            @RequestParam(value = "privilege") Boolean privilege,
            @RequestParam(value = "emails", required = false) String emails,
            @RequestParam(value = "teamId", required = false, defaultValue = "-1") Integer teamId,
            @RequestPart(value = "src") MultipartFile src) {
        Result<String> url = baseFeignClient.upload(src);
        Assert.isTrue(url.getCode().equals(ResultCode.SUCCESS.getCode()), "添加团队图片失败");
        String formerUrl = "";
        if (teamId != -1) {
            formerUrl = teamService.getById(teamId).getSrc();
        }
        teamService.insertTeam(userId, title, description, status ? 1 : 0, privilege ? 1 : 0, teamId, emails, url.getData());
        if (!"".equals(formerUrl)) {
            baseFeignClient.delete(formerUrl);
        }
        return Result.succeed(teamId == -1 ? "添加团队成功" : "更新团队成功 ", true);
    }

    /**
     * 管理员更新团队状态
     *
     * @param status 状态
     * @param teamId 团队ID
     * @return Result<Boolean>
     * @author CK
     * @date 2021/5/5 19:40
     */
    @PostMapping("/updateTeamStatus")
    public Result<Boolean> updateTeamStatus(
            @RequestParam(value = "status") Integer status,
            @RequestParam(value = "teamId") Integer teamId) {
        Team team = new Team();
        team.setTeamId(teamId).setStatus(status ^ 1);
        boolean flag = teamService.updateById(team);
        Assert.isTrue(flag, "更新状态失败");
        return Result.succeed("更新状态成功", true);
    }

    /**
     * 管理员查询团队列表
     *
     * @param current 当前页
     * @param size    页面大小
     * @param name    搜索名
     * @return Result<Page < Team>>
     * @author CK
     * @date 2021/5/5 18:25
     */
    @GetMapping("/teamListAdmin")
    public Result<Page<Team>> teamListAdmin(
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "name") String name) {
        Page<Team> page = teamService.teamListAdmin(current, size, name);
        return Result.succeed("查询团队列表成功", page);
    }

    /**
     * 管理员根据团队ID查询团队信息
     *
     * @param teamId 团队ID
     * @return Result<Team>
     * @author CK
     * @date 2021/5/5 19:56
     */
    @GetMapping("/teamByIdAdmin")
    public Result<TeamAdminVO> teamByIdAdmin(@RequestParam(value = "teamId") Integer teamId) {
        TeamAdminVO team = teamService.teamByIdAdmin(teamId);
        Assert.notNull(team, "查询团队信息失败");
        return Result.succeed("查询团队信息成功", team);
    }

    /**
     * 管理员查询简要团队数据
     *
     * @return Result<List < Team>>
     * @author CK
     * @date 2021/5/7 20:36
     */
    @GetMapping("/listSimpleTeam")
    public Result<List<Team>> listSimpleTeam() {
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("team_id", "title");
        List<Team> list = teamService.list(queryWrapper);
        Assert.notNull(list, "查询简要团队数据失败");
        return Result.succeed("查询简要团队数据成功", list);
    }

    /**
     * 用户查询团队列表
     *
     * @param current 当前页
     * @param size    分页数
     * @return Result<Team>
     * @author CK
     * @date 2021/5/8 14:23
     */
    @GetMapping("/listTeam")
    public Result<Page<Team>> listTeam(
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            @RequestParam(value = "size", required = false, defaultValue = "8 ") Integer size) {
        Page<Team> page = new Page<>(current, size);
        Page<Team> page1 = teamService.listTeam(page);
        Assert.notNull(page1, "查询团队失败");
        return Result.succeed("查询团队成功", page1);
    }

    /**
     * 管理员删除团队
     *
     * @param teamId 团队ID
     * @return Result<Boolean>
     * @author CK
     * @date 2021/5/8 18:50
     */
    @PostMapping("/deleteTeam")
    public Result<Boolean> deleteTeam(@RequestParam(value = "teamId") Integer teamId) {
        teamService.removeTeam(teamId);
        return Result.succeed("删除团队成功", true);
    }

}

